﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Data.Objects;
using System.Text;
using System.Collections.Generic;

namespace Windows_Phone_7_Web_Application
{
    [ServiceContract(Namespace = "")]
    [SilverlightFaultBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Windows_Phone_7_Service
    {
        [OperationContract]
        public List<DateTime?> GetAvailability(string text)
        {
            using (LicentaRovigneteEntities entities = new LicentaRovigneteEntities())
            {
                ObjectResult<SP_Get_Availability_Result> res = entities.SP_Get_Availability(text);
                SP_Get_Availability_Result r = res.FirstOrDefault();
                return new List<DateTime?> {r.Start, r.End};
            }
        }

        [OperationContract]
        public bool? CheckAvailabilityDuplicates(DateTime start, string nrInm)
        {
            using (LicentaRovigneteEntities entities = new LicentaRovigneteEntities())
            {
                ObjectResult<bool?> res = entities.SP_Check_Availability_Duplicates(start, nrInm);
                return res.FirstOrDefault();
            }
        }

        [OperationContract]
        public void InsertRecord(
            String VehiclePlate
            , String VehicleCountry
            , String VehicleType
            , String Valability
            , DateTime ValabilityStartDate
            , String OwnerName
            , String OwnerAlias
            , String SocialSecurityNumber
            , String Adress
            , String PhoneNumber
            , String Email
            , String ClientCity
            , String ClientProvince
            , String CardType
            , String CardOwner
            , String CardNumber
            , String CardSecurityNumber
            , String TotalCosts
            , bool PersonType
            , String ClientCountry
            )
        {
            using (LicentaRovigneteEntities entities = new LicentaRovigneteEntities())
            {
                entities.SP_Insert_Record(
                    VehiclePlate
                    , String.IsNullOrWhiteSpace(VehicleCountry) ? null : VehicleCountry
                    , String.IsNullOrWhiteSpace(VehicleType) ? null : VehicleType 
                    , String.IsNullOrWhiteSpace(OwnerName) ? null : OwnerName 
                    , String.IsNullOrWhiteSpace(OwnerAlias) ? null : OwnerAlias 
                    , String.IsNullOrWhiteSpace(ClientCountry) ? null : ClientCountry
                    , ClientProvince 
                    , ClientCity 
                    , Adress 
                    , Email 
                    , PhoneNumber 
                    , String.IsNullOrWhiteSpace(SocialSecurityNumber) ? null : SocialSecurityNumber 
                    , "RD000000" // not given
                    , "Sectia XXX" // not given
                    , null      // not given
                    , null      // not given
                    , null      // not given
                    , null      // not given
                    , null      // not given
                    , PersonType 
                    , Valability 
                    , ValabilityStartDate 
                    , "EUR"     // STANDARD
                    , "CB"      // STANDARD
                    , null      // for future use
                    , "MOB"     // STANDARD
                    , String.IsNullOrWhiteSpace(EncryptDecryptInfo(CardNumber, encodeDecodeKey, enEnCryptDecrypt.DECRYPT)) ? null : EncryptDecryptInfo(CardNumber, encodeDecodeKey, enEnCryptDecrypt.DECRYPT) 
                    , String.IsNullOrWhiteSpace(CardType) ? null : CardType 
                    , null      // TODO
                    , String.IsNullOrWhiteSpace(EncryptDecryptInfo(CardSecurityNumber, encodeDecodeKey, enEnCryptDecrypt.DECRYPT)) ? null : EncryptDecryptInfo(CardSecurityNumber, encodeDecodeKey, enEnCryptDecrypt.DECRYPT) 
                    , String.IsNullOrWhiteSpace(EncryptDecryptInfo(CardOwner, encodeDecodeKey, enEnCryptDecrypt.DECRYPT)) ? null : EncryptDecryptInfo(CardOwner, encodeDecodeKey, enEnCryptDecrypt.DECRYPT) 
                    );
            }
        }

        private String encodeDecodeKey = "alba-ca-zapada-si-cei-sapte-pitici";
        private enum enEnCryptDecrypt
        {
            DECRYPT, ENCRYPT
        }; 

        private string EncryptDecryptInfo(String Message, String Key, enEnCryptDecrypt EnC)
        {
            int messageCount, keyCount, value = 0;
            StringBuilder strRetVal = new StringBuilder();

            for (messageCount = 0, keyCount = 0; messageCount < Message.Length; messageCount++, keyCount++)
            {
                if (keyCount >= Key.Length)
                {
                    keyCount = 0;
                }

                // Rotates the Key string counter to Zero when character value 
                // is traversed till end
                if (EnC == enEnCryptDecrypt.ENCRYPT)
                {
                    //When Encryption is required we add ASCII value of key with Message
                    value = (Message[messageCount]) + (Key[keyCount]);
                }
                else
                {
                    //When Decryption is required we subtract ASCII value of key with Message
                    value = (Message[messageCount]) - (Key[keyCount]);
                }

                // Here we convert the ASCII value to character and Append that to Stringbuilder
                strRetVal.Append((char)(value));
            }

            //Returns the Processed String.
            return strRetVal.ToString();
        }
    }
}
